package com.xingxue.logistics.controller.sys;

import com.github.pagehelper.PageInfo;
import com.xingxue.logistics.common.vo.Page;
import com.xingxue.logistics.common.vo.RestResult;
import com.xingxue.logistics.dao.domain.SysRole;
import com.xingxue.logistics.service.RoleService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Kris at 2018/10/9 15:57
 */
@RestController
@RequestMapping("sys/role")
@Api(description = "角色相关控制器")
public class RoleController {

    @Autowired
    RoleService roleService;


    @ApiOperation("分页查询角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "查询条件：角色ID", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "type", value = "查询条件：角色类型", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "name", value = "查询条件：角色名称", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页码", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "rows", value = "每页长度", dataTypeClass = Integer.class, paramType = "query"),
    })
    @GetMapping("search_page")
    public ResponseEntity<RestResult<PageInfo<SysRole>>> searchPage(HttpServletRequest request) {
        String id_str = request.getParameter("id");
        String type_str = request.getParameter("type");
        String page_str = request.getParameter("page");
        String rows_str = request.getParameter("rows");

        Integer id = null, type = null, page = null, rows = null;
        if (StringUtils.isNotBlank(id_str)) {
            id = Integer.parseInt(id_str);
        }
        if (StringUtils.isNotBlank(type_str)) {
            type = Integer.parseInt(type_str);
        }
        if (StringUtils.isNotBlank(page_str)) {
            page = Integer.parseInt(page_str);
        }
        if (StringUtils.isNotBlank(rows_str)) {
            rows = Integer.parseInt(rows_str);
        }
        String name = request.getParameter("name");
        PageInfo<SysRole> rolePage = roleService.searchPage(id, name, type, page, rows);
        return ResponseEntity.ok(new RestResult<>(true, "数据加载完成", rolePage));
    }


    @ApiOperation("分页查询角色（easyUI用）")
    @GetMapping("search_page_easyui")
    public ResponseEntity<Page<SysRole>> searchPageEasyUI(
            @RequestParam(required = false) @ApiParam("查询条件：角色ID") Integer id,
            @RequestParam(required = false) @ApiParam("查询条件：角色名称") String name,
            @RequestParam(required = false) @ApiParam("查询条件：角色类型") Integer type,
            @RequestParam(required = false) @ApiParam("当前页码") Integer page,
            @RequestParam(required = false) @ApiParam("每页行数") Integer rows
    ) {
        PageInfo<SysRole> rolePage = roleService.searchPage(id, name, type, page, rows);
        return ResponseEntity.ok(new Page(rolePage));
    }


    @ApiOperation("新增角色")
    @PostMapping
    public ResponseEntity<RestResult> addRole(@RequestBody SysRole role) {
        int i = roleService.add(role);
        if(i>0){
            return ResponseEntity.ok(new RestResult(true,"新增成功",null));
        }
        return ResponseEntity.ok(new RestResult(false,"新增失败",null));
    }

    @ApiOperation(value = "修改角色",notes = "根据ID修改")
    @PutMapping
    public ResponseEntity<RestResult> updateRole(@RequestBody SysRole role) {
        int i = roleService.update(role);
        if(i>0){
            return ResponseEntity.ok(new RestResult(true,"修改成功",null));
        }
        return ResponseEntity.ok(new RestResult(false,"修改失败",null));
    }


    @ApiOperation("删除角色")
    @DeleteMapping("{id}")
    public ResponseEntity<RestResult> addRole(@PathVariable("id") @ApiParam(value = "要删除的角色ID",required = true) Integer id) {
        int i = roleService.delete(id);
        if(i>0){
            return ResponseEntity.ok(new RestResult(true,"删除成功",null));
        }
        return ResponseEntity.ok(new RestResult(false,"删除失败",null));
    }



}
